/**
 * Created by zzy on 2017/1/6
 */
var $ = require('jquery');
var $alert = require('libs/alert');
require('easyui')
var article_type={"1":"平台动态","2":"行业动态","3":"永康动态","4":"通知公告","5":"政策法规",
    "6":"系统公告","7":"服务中心","8":"常见问题","9":"合作流程","10":"咨询与视频","11":"设备购置","12":"设备人员配置","13":"设备人员要求"};
var status={"1":"已发布","2":"未发布"};
var isSale = require('libs/user-type').isSale;
$(function () {
    $("#a_create").show();
    $("#a_remove").show();
    articleLevelBoxInit();
    mainTableInit();
    $("#a_remove").on("click",function () {
        var selects = $("#main_table").datagrid("getSelections");
        if(selects.length==0){
            $alert("请选择文章!");
            return false;
        }
        var idStr = "";
        $.each(selects,function (index,obj) {
            if(idStr!=""){
                idStr = idStr+","
            }
            idStr = idStr+obj.id;
            $.ajax({
                url:"api/v1/p/mms/article/delete",
                type:"post",
                dataType:"json",
                data:{
                    idStr:idStr
                },
                success:function (result) {
                    if(result.code=="200") {
                        $alert("删除成功!");
                        $("#main_table").datagrid("reload");
                    }else{
                        $alert(result.message);
                    }
                }
            })
        });
    });
    $("#a_search").on("click",function () {
        console.log($('#title').val())
        $('#main_table').datagrid('load',{
            title:$('#title').val()+"",
            isHead:$('#select_article_level').combobox('getValue')
        });
        /*        $('#select_logistics').combobox('clear');
         $('#select_warehouse').combobox('clear');
         $('#input_from_date').datebox('setValue','');
         $('#input_to_date').datebox('setValue','');*/
    });
    $("body").on("click","a[name=release]",function(){
        var id = $(this).parents("tr").children().eq(0).children().children().val();
        $.ajax({
            url:"api/v1/p/mms/article/release",
            type:"post",
            dataType:"json",
            data:{
                id:id
            },
            success:function (result) {
                if(result.code=="200") {
                    $alert("发布成功!");
                    $("#main_table").datagrid("reload");
                }else{
                    $alert(result.message);
                }
            }
        })
    });
    $("body").on("click","a[name=delete]",function(){
        var id = $(this).parents("tr").children(":first").find("input[type=checkbox]").val();
        $.ajax({
            url:"api/v1/p/mms/article/delete",
            type:"post",
            dataType:"json",
            data:{
                idStr:id
            },
            success:function (result) {
                if(result.code=="200") {
                    $alert("删除成功!");
                    $("#main_table").datagrid("reload");
                }else{
                    $alert(result.message);
                }
            }
        })
    });
    $("body").on("click","a[name=edit]",function(){
        var id = $(this).parents("tr").children(":first").find("input[type=checkbox]").val();
        var href = "article-update?id="+id;
        window.location.href = href;
    })
    $("body").on("click","a[name=title]",function(){
        var id = $(this).parents("tr").children(":first").find("input[type=checkbox]").val();
        var href = "article-detail?id="+id;
        window.location.href = href;
    })
});
function articleLevelBoxInit() {
    $("#select_article_level").combobox({
        valueField: 'value',
        textField: 'label',
        data: [{
            label: '请选择',
            value: ''
        },{
            label: '头条',
            value: '1'
        },{
            label: '普通',
            value: '0'
        }]
    })
}
function mainTableInit() {
    $("#main_table").datagrid({
        url:'api/v1/p/mms/article/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        selectOnCheck : true,
        emptyMsg : "暂无数据",
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                checkbox:true
            },{
                field:'title',
                title:'文章标题',
                width:250,
                formatter:function (value,row,index) {
                    return "<a href='javascript:;' name='title'>"+value+"</a>"
                }
            },{
                field:'type',
                title:'所属类别',
                width:100,
                formatter:function (value,row,index) {
                    return article_type[value]
                }
            },{
                field:'createdDt',
                title:'创建日期',
                width:200
            },{
                field:'releaseTime',
                title:'发布日期',
                width:200
            },{
                field:'stat',
                title:'发布状态',
                width:100,
                formatter:function (value,row,index) {
                    if(value=="2"){
                        return "<span style='color:red';>"+status[value]+"</span>";
                    }else{
                        return status[value]
                    }
                }
            },{
                field:'opertation',
                title:'操作',
                hidden:isSale,
                width:200,
                formatter:function (value,row,index) {
                    if(row.stat=="2"){
                        return "<a href='javascript:;' name='release'>发布</a>|<a href='javascript:;' name='edit'>编辑</a>|<a href='javascript:;' name='delete'>删除</a>";
                    }else{
                        return "<a href='javascript:;' name='edit'>编辑</a>|<a href='javascript:;' name='delete'>删除</a>";
                    }
                }
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
}